export default {
  data() {
    return {
      // 列表加载状态
      loading: false,
      // 列表加载完成
      finished: false,
      // 列表加载失败
      error: false,
      // 下拉刷新列表
      refreshing: false,
      listData: []
    }
  },
  methods: {
    getList(page) {
      if (page) {
        this.finished = false
        this.listData = []
        this.searchForm.pageNumber = page
      }
      this.searchForm.recordSize = this.searchForm.pageSize * (this.searchForm.pageNumber - 1)
      this.loading = true
      this.api
        .list(this.searchForm)
        .then(res => {
          const { status, data } = res.data
          if (status === 0) {
            // 下拉刷新完成
            if (this.refreshing) this.refreshing = false
            // 数据加载完成
            this.loading = false
            if (data.count === 0) {
              // 没有数据
              this.finished = true
            } else {
              this.listData.push(...data.list)
              if (Math.ceil(data.count / this.searchForm.pageSize) === this.searchForm.pageNumber) {
                // 最后一页
                this.finished = true
              } else {
                // 下一页
                this.searchForm.pageNumber++
              }
            }
          } else {
            this.loading = false
            this.error = true
          }
        })
        .catch(() => {
          this.loading = false
          this.error = true
        })
    }
  }
}
